from rest_framework import serializers
from rest_framework.exceptions import ValidationError
import re
# def phone_validator(value):
#     if re.match(r'^1[3|4|5|6|7|8|9]\d{9}$',value):
#
#         return
#     else:
#         raise ValidationError('手机号格式错误')
#
# class MessageSerializer(serializers.Serializer):
#
#     # phone=serializers.CharField(label='手机号',validators=[phone_validator,])
#     phone=serializers.CharField()
#
#     def validate_phone(self,phone):
#         if re.match(r'^1[3|4|5|6|7|8|9]\d{9}$',phone):
#
#             return phone
#         else:
#             raise ValidationError('手机号错误')
#     # def validate_phone(self, phone):
#     #     pass
#
def validate_phone(phone):
    if re.match(r'^1[3|4|5|6|7|8|9]\d{9}$',phone):

        return None
    else:

        raise ValidationError('手机号格式错误')
class MessageSerializer(serializers.Serializer):
    phone=serializers.CharField(validators=[validate_phone,])